package com.hz.house.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hz.house.bean.entity.OfficialAccountbean;
import com.hz.house.dao.HotQaMapper;

/**
 * 公共一些方法接口封装
 * @author lfw
 * 
 *
 */
@Service
public class CommonService {

	@Resource
	private RestTemplate ribbonTemplate;
	
	@Autowired
	private HotQaMapper bbsMapper;
	
	@Autowired
	private BbsCacheService bbsCacheService;
	
	private final String BUILDING_SERVICE_URL=  "http://house-small-building/ms/building";
	
	/**检查文字内容是否包含敏感词汇:true没有，false有*/
	public boolean checkContent(String content) {
    	//ribbonTemplate
    	String url = this.BUILDING_SERVICE_URL+"/checkTextContent";
    	String res = this.doPostSendMsg(content, url);
    	System.out.println(res);
    	JSONObject jo = JSON.parseObject(res);
    	if(jo.getIntValue("code")!=200)
    		return false;
    	return jo.getBooleanValue("data");
    	 
    }
	
	
	/**封装http请求发送post,返回接口的结果 */
	private String doPostSendMsg(String data,String url) {
		String json = ribbonTemplate.postForObject(url, data, String.class);
		return json;
	}
	
	
	/**判断用户是不是官方用户*/
	public boolean isUserOfficialAccount(int customerId) {
		List<OfficialAccountbean> ofs = this.bbsCacheService.getOfficialAccount();
		if(ofs==null) 
			ofs = initOfficialAccount();
		if(ofs.size()==0)
			return false;
		for(OfficialAccountbean o:ofs) {
			if(o.getCustomerId().intValue()==customerId)
				return true;
		}
		return false;
	}
	
	
	public List<OfficialAccountbean> initOfficialAccount(){
		List<OfficialAccountbean> all  = this.bbsMapper.getAllOfficialAccount();
		this.bbsCacheService.setOfficicalAccountCache(all);
		return all;
	}
	

	 public String getXiaoChengxuToken() {
		  String url = "http://house-small-building/ms/building/getXiaoChengxuTk";
		  String res = this.ribbonTemplate.getForObject(url, String.class);
		  System.out.println(res);
		  JSONObject jo = JSON.parseObject(res);
		  return  jo.getString("data");
	  }
}
